<template>
	<view class="loveDetail">
		<img src="/static/love_bg2.jpg" style="position:absolute;top: 0; left: 0;width:100%;min-height:110vh;" />
		<swiper class="card-swiper square-dot" :indicator-dots="true" :circular="true"
		 :autoplay="true" interval="5000" duration="500" @change="cardSwiper" indicator-color="#8799a3"
		 indicator-active-color="#0081ff">
			<swiper-item v-for="(item,index) in info.photos" :key="index" :class="cardCur==index?'cur':''">
				<view class="swiper-item" @tap="previewImage(index)">
					<image :src="item" mode="aspectFit"></image>
				</view>
			</swiper-item>
		</swiper>
		<view class="cu-list menu card-menu margin-top">
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-title text-orange"></text>
					昵称: <text class="text-grey padding-left">{{info.nick_name}}</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-title text-orange"></text>
					性别: <text class="text-grey padding-left">{{info.gender}}</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-title text-orange"></text>
					年龄: <text class="text-grey padding-left">{{calculateAge(info.birthday)}}岁</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-title text-orange"></text>
					所在位置: <text class="text-grey padding-left" v-if="info.region">
					{{info.region[0]}} {{info.region[1]}} {{info.region[2]}}
					</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-title text-orange"></text>
					身高: <text class="text-grey padding-left">{{info.height}}cm</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-title text-orange"></text>
					体重: <text class="text-grey padding-left">{{info.weight}}</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-title text-orange"></text>
					学历: <text class="text-grey padding-left">{{info.degree}}</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-title text-orange"></text>
					收入: <text class="text-grey padding-left">{{info.income}}</text>
				</view>
			</view>
			<view class="cu-item" v-if="invite_status == 1 || other_invite_status == 1">
				<view class="content">
					<text class="cuIcon-title text-orange"></text>
					小红书号: <text class="text-grey padding-left">{{info.red_book_id}}</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-title text-orange"></text>
					性格: <text class="text-grey padding-left">{{info.character}}</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-title text-orange"></text>
					爱好: <text class="text-grey padding-left">{{info.hobby}}</text>
				</view>
			</view>
			<view class="cu-item">
				<view class="content">
					<text class="cuIcon-title text-orange"></text>
					对另一半期望: <text class="text-grey padding-left">{{info.other_expect}}</text>
				</view>
			</view>
			<view class="padding flex flex-direction" v-if="invite_status == -1 && other_invite_status == -1">
				<button class="cu-btn bg-red margin-tb-sm lg" @tap="inviteContact()">邀请联系</button>
			</view>
			<view class="padding flex flex-direction" v-if="invite_status >= 0">
				<button class="cu-btn bg-red margin-tb-sm lg" v-if="invite_status == 0">等待对方同意</button>
				<button class="cu-btn bg-green margin-tb-sm lg" v-if="invite_status == 1">对方已同意</button>
				<button class="cu-btn bg-grey margin-tb-sm lg" v-if="invite_status == 2">对方已拒绝</button>
			</view>
			<view class="padding flex flex-direction" v-if="other_invite_status >= 0">
				<button class="cu-btn bg-orange margin-tb-sm lg" v-if="other_invite_status == 0" @tap="acceptContact()">接受邀请</button>
				<button class="cu-btn bg-red margin-tb-sm lg" v-if="other_invite_status == 0" @tap="refuseContact()">拒绝邀请</button>
				<button class="cu-btn bg-green margin-tb-sm lg" v-if="other_invite_status == 1">你已同意</button>
				<button class="cu-btn bg-grey margin-tb-sm lg" v-if="other_invite_status == 2">你已拒绝</button>
			</view>
		</view>
		<view class="cu-modal" catchtouchmove='true' :class="modalName=='Image'?'show':''" @tap="hideModal">
			<view class="cu-dialog" @tap.stop="">
				<image :src="modalImg" mode="widthFix"></image>
			</view>
		</view>
	</view>
</template>

<script>
	import { profileInfo } from '../../api/account';
	import { objectInfo, objectInviteContact, AgreeOrRefuseInvite } from '../../api/object';
	export default {
		data() {
			return {
				cardCur: 0,
				dotStyle: false,
				info: {},
				invite_status: -1,
				other_invite_status: -1,
				profileEmpty: false,
				modalName: null,
				modalImg: null,
			};
		},
		onLoad(query) {
			// this.TowerSwiper('swiperList');
			// 初始化towerSwiper 传已有的数组名即可
			profileInfo().then(res => {
				if (res == undefined || (res.code != undefined && res.code == 1001)) {
					this.profileEmpty = true
					return
				}
			}).catch(err => {
				console.log(err)
				this.profileEmpty = true
				return
			});
			
			// 查询资料
			objectInfo({account_id: query.id}).then(res => {
				if(res.info == undefined) {
					uni.navigateBack()
					return
				}
				this.info = res.info
				this.invite_status = res.invite_status
				this.other_invite_status = res.other_invite_status
				
				this.info.photos = [this.info.avatar,...this.info.photos]
			}).catch(err => {
				console.log(err)
				uni.navigateBack()
			});
		},
		methods: {
			cardSwiper(e) {
				this.cardCur = e.detail.current
			},
			showModal(src) {
				this.modalImg = src
				this.modalName = "Image"
			},
			hideModal(e) {
				this.modalName = null
			},
			previewImage(index) {
				uni.previewImage({
					current: index,
					indicator: "none",
					loop: false,
					urls: this.info.photos,
					longPressActions: {},
					sizeType: ['original']
				})
			},
			calculateAge(birthDateStr) {
				var birthDate = new Date(birthDateStr)
				var currentDate = new Date();
				var birthYear = birthDate.getFullYear();
				var birthMonth = birthDate.getMonth();
				var birthDayOfMonth = birthDate.getDate();
				var currentYear = currentDate.getFullYear();
				var currentMonth = currentDate.getMonth();
				var currentDayOfMonth = currentDate.getDate();
				 
				var age = currentYear - birthYear;
				if (currentMonth < birthMonth || (currentMonth == birthMonth && currentDayOfMonth < birthDayOfMonth)) {
				    age--;
				}
				return age;
			},
			inviteContact() {
				// if(this.profileEmpty) {
				// 	uni.showToast({
				// 		title: "请完善资料后再发起邀请",
				// 		icon: 'none',
				// 		duration: 2000
				// 	});
				// 	return
				// }
				
				objectInviteContact({account_id: this.info.account_id}).then(res => {
					if (res != undefined &&res.ok != undefined) {
						uni.showToast({ title: '邀请成功，耐心等待对方回应', icon: 'none' });
						this.invite_status = 0
						return
					} else {
						if (res != undefined && res.msg != undefined) {
							uni.showToast({ title: res.msg, icon: 'none' });
							return
						}
						uni.showToast({ title: '邀请失败', icon: 'none' });
						return
					}
				}).catch(err => {
					console.log(err)
				});
			},
			acceptContact() {
				AgreeOrRefuseInvite({account_id: this.info.account_id, status: 1}).then(res => {
					if (res != undefined && res.msg != undefined) {
						uni.showToast({ title: res.msg, icon: 'none' });
						return
					}
					this.other_invite_status = 1
					uni.showToast({ title: '已结缘分, 请查看对方联系', icon: 'none' });
					return
				}).catch(err => {
					console.log(err)
				});
			},
			refuseContact() {
				AgreeOrRefuseInvite({account_id: this.info.account_id, status: 0}).then(res => {
					if (res != undefined && res.msg != undefined) {
						uni.showToast({ title: res.msg, icon: 'none' });
						return
					}
					this.other_invite_status = 2
					uni.showToast({ title: '已拒绝邀请', icon: 'none' });
					return
				}).catch(err => {
					console.log(err)
				});
			}
		}
	}
</script>

<style scoped>

</style>